import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
    selector: 'sa-loadometer',
    templateUrl: './loadometer.component.html',
    styleUrls:['./loadometer.component.css']
})
export class LoadometerComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	carList:any[] = [];
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	deviceId:string = '';//设备ID
	carNumber:string = ''; //车牌号码
	startTime:string = '';
	endTime:string = '';
	abnormalType:string='';//警报分类
	useTypeL1:string='';//车辆用途
	useTypeL2:string='';//车辆分类
	isDetail:string = '';//处理状态
	carTypeDetailList:any = [];//useTypeL2数据
	carTypeList:any = [];
	carUseList:any = {};
	carDeatailUseList:any = {};
	
	activeSelected:string='';
	startTime_chart:string='';
	endTime_chart:string='';
	isChartView:boolean=false;
	//地图折线组件所需参数 start
	selectedRow:any;//折线子组件获取数据的参数
	drawTrackArr:any;//折线子组件传回   地图子组件画轨迹的数据
	isMapView=false;//折线图子组件选中是否展开地图
	
	specifyId={
		picker1:'datetimepicker3',
		picker2:'datetimepicker4'
	}
	specifyTime={
		startTime:'',
		endTime:'',
	}
	editRowParams:any={
		id:'',
		carNumber:'',
		bridgeDate:'',
		type:'1',
		isDetail:'0',
		remark:'',
	};
	
	
	//地图折线组件所需参数 end
	constructor(private jsonApiService: JsonApiService,private router: Router,private activatedRoute: ActivatedRoute,) {
		activatedRoute.queryParams.subscribe(queryParams => {
			if(queryParams.nowPage){
				this.curPage = queryParams.nowPage;
			}
		});
	}
	//获取选中的公司与车辆号码
	getSelectTableTime(time){
		this.startTime=time.startTime;
		this.endTime=time.endTime;
	} 
	getSelectChartTime(time){
		this.startTime_chart=time.startTime;
		this.endTime_chart=time.endTime;
	}
	//地图折线组件方法 start
	runParent(value){
		// 接收子组件传递过来的数据 
	   	this.drawTrackArr=value;
	}
	showMapView(val){
		this.isMapView=val;
	}
	drawAbnormalChart(){
		if(!this.selectedRow){
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if(!this.startTime_chart||!this.endTime_chart){
			this.jsonApiService.message('异常起始时间不能为空');
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime_chart,this.endTime_chart,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let params={
			deviceId:this.selectedRow.deviceId,
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
		}
		this.selectedRow=params;
		
	}
	checkAbnormal(row){
		this.activeSelected=row.id;
		this.startTime_chart=common.getTimeMmss( new Date(common.mmsstoTimestamp(row.bridgeDate)-5*60*60*1000) );
		this.endTime_chart=common.getTimeMmss( new Date(common.mmsstoTimestamp(row.bridgeDate)+5*60*60*1000) );
		this.isChartView=true;
		row.startTime=this.startTime_chart;
		row.endTime=this.endTime_chart;
		this.specifyTime={
			startTime:this.startTime_chart,
			endTime:this.endTime_chart,
		}
		this.selectedRow=row;
	}
	del_chartDate(){
		this.startTime_chart = '';
		this.endTime_chart ='';
	}
	closeChart(){
		this.isChartView=false;
	}
	//地图折线组件方法 end
//	editRow(row){
//		this.router.navigate(['/abnormal-alert/loadometer/loadometer-page'],{queryParams: {id: row.id,type: 'updata',nowPage:this.curPage}});
//	}

	editRow(row){
		for (let item in this.editRowParams){
			this.editRowParams[item]=row[item];
		}
		
	}
	submitUpdate(val){
		let reqdata = 'id='+this.editRowParams.id+'&status='+this.editRowParams.isDetail+'&remark='+this.editRowParams.remark;
		this.jsonApiService.http("alertweigherror/editAlertForWeight.html",reqdata).subscribe(result=> {
			this.jsonApiService.message(result.message);
			if(result.success){
				this.load();
				val.hide();
			}
		})
	}
	ngOnInit() {
		this.initSeletctList();
		this.load();
	}
	//车辆用途change回调
	carUseTypeL1Change(){
		this.carTypeDetailList = [];
		this.useTypeL2 = '';
		for(let item of this.carTypeList){
			if(item.id === this.useTypeL1){
				this.carTypeDetailList = item.children;
				break;
			}
		}
	}
	delete_row(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("alertweigherror/deleteDataInfoById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//  初始化加载list数据
	load() {
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				startTime:this.startTime,
				endTime:this.endTime,
				searchKey:this.searchKey,
				type:this.abnormalType,
				deviceId:this.deviceId,
				companyId:this.selectedCompanyId,
				useTypeL1:this.useTypeL1,
				useTypeL2:this.useTypeL2,
				isDetail:this.isDetail
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("alertweigherror/pageinfoList.html ", reqdata).subscribe(result => {
			if(!result.isSuccess){
				this.jsonApiService.message(result.message);
				return;
			}
			for(let item of result.exhibitDatas){
				item.useTypeL1 = this.getUseTypeNameL1(item.useTypeL1);
				item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
		})
	}
	
	//key值映射
	getUseTypeNameL1(key){
		if(key){
			return this.carUseList[key];
		}else{
			return '';
		}
	}
	//key值映射
	getUseTypeNameL2(key){
		if(key){
			return this.carDeatailUseList[key];
		}else{
			return '';
		}
	}
	ngOnDestroy(){
		document.onclick = null;
	}
	//刷新
    refresh(){
    	this.load();
    }

	//查询
	table_search(){
		this.curPage=1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
		//初始化车辆用途,车辆分类选项数据
	initSeletctList(){
		this.carTypeList = carTypeListObj.carTypeList
		this.carUseList = carTypeListObj.carUseList
		this.carDeatailUseList = carTypeListObj.carDeatailUseList
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
}
